package com.sunagy.demand.backend.controller;

import com.sunagy.demand.backend.VO.request.AdminUserAddRequestVO;
import com.sunagy.demand.backend.VO.request.AdminUserLoginRequestVO;
import com.sunagy.demand.backend.VO.ResultVO;
import com.sunagy.demand.backend.entity.AdminUser;
import com.sunagy.demand.backend.VO.response.AdminUserResponseVO;
import com.sunagy.demand.backend.service.AdminAuthTokenService;
import com.sunagy.demand.backend.service.AdminUserService;
import com.sunagy.demand.backend.utils.ResultVOUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * desc：#TODO fill me ！
 * author：三餘無夢生
 * date：2018/4/24
 * time：下午6:17
 */
@Slf4j
@RestController
@RequestMapping("/adminUser")
@Api(value = "管理端帐号模块", description = "管理员帐号的增删改查和登录接口")
public class AdminUserController {

    @Autowired
    private AdminUserService adminUserService;

    @Autowired
    private AdminAuthTokenService adminAuthTokenService;

    @ApiOperation(value = "查询管理员", notes = "查询所有管理员")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultVO<List<AdminUserResponseVO>> getAll() {
        List<AdminUserResponseVO> adminUserResponseVOList = adminUserService.getAll();
        return ResultVOUtil.success(adminUserResponseVOList);
    }

    @ApiOperation(value = "创建管理员", notes = "根据管理员信息创建管理员帐号")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<AdminUserResponseVO> add(@RequestBody AdminUserAddRequestVO adminUserAddRequestVO) {
        AdminUserResponseVO adminUserResponseVO = adminUserService.add(adminUserAddRequestVO);
        return ResultVOUtil.success(adminUserResponseVO);
    }


    @ApiOperation(value = "管理员登录", notes = "管理员通过用户名密码登录")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultVO<Map<String,String>> login(@RequestBody AdminUserLoginRequestVO adminUserLoginRequestVO) {
        AdminUserResponseVO adminUserResponseVO = adminUserService.login(adminUserLoginRequestVO.getUsername(),adminUserLoginRequestVO.getPassword());
        String token = adminAuthTokenService.updateToken(adminUserResponseVO);
        Map<String,String> map = new HashMap<>();
        map.put("token",token);
        return ResultVOUtil.success(map);
    }

    @ApiOperation(value = "删除管理员", notes = "根据id删除管理员")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResultVO<Map<String,String>> addUser(@PathVariable Integer id) {
        AdminUserResponseVO adminUserResponseVO = adminUserService.delete(id);
        Map<String,String> map = new HashMap<>();
        map.put("id",adminUserResponseVO.getId().toString());
        return ResultVOUtil.success(map);
    }

    @ApiOperation(value = "编辑管理员", notes = "根据id编辑管理员")
    @ApiResponses({
            @ApiResponse(code = 500, message = "接口异常")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResultVO<Map<String,String>> addUser(@RequestBody AdminUser adminUser, @PathVariable Integer id) {
        AdminUserResponseVO adminUserResponseVO = adminUserService.edit(id,adminUser);
        Map<String,String> map = new HashMap<>();
        map.put("id",adminUserResponseVO.getId().toString());
        return ResultVOUtil.success(map);
    }
}
